<template>
  <ant-modal
    modalWidth="1300"
    modalHeight="600"
    :visible="open"
    modal-title="选择产品"
    :adjust-size="true"
    @cancel="cancel"
  >
    <a-row slot="content">
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6" >
              <a-form-item label="查询条件">
                <a-input v-model="queryParam.orderParams" placeholder="请输入查询条件" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="产品大类">
                <a-tree-select
                  v-model="queryParam.categoryId"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="categoryIdOptions"
                  placeholder="请选择"
                  :replaceFields="{ children: 'children', title: 'categoryName', key: 'id', value: 'id' }"
                  tree-default-expand-all
                  @change="handleQuery"
                />
              </a-form-item>
            </a-col>

            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-divider />
    <advance-table
      :components="drag(columns,'productList')"
      columnsTitle="productList"
      :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
      tableKey="base-bizProduct-index-table"
      @change="handleTableChange"
      rowKey="id"
      size="middle"
      @refresh="getList"
      :columns="columns"
      :data-source="bizProductList"
      :loading="loading"
      :format-conditions="true"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
    
    <span slot="inventoryFlagSlot" slot-scope="{record}">
      <a-tag color="green" v-if="record.inventoryFlag === '1'">
        是
      </a-tag>
      <a-tag color="red" v-else-if="record.inventoryFlag === '0'">
        否
      </a-tag>
      <a-tag color="orange" v-else>
        未设置
      </a-tag>
    </span>
    </advance-table>
      <biz-product-add-form
        v-if="showAddModal"
        ref="bizProductAddForm"
        :categoryIdOptions="categoryIdOptions"
        :warehouseList="warehouseList"
        :inventoryOptions="inventoryOptions"
        @ok="addProductOk"
        @select-tree="getTreeSelect"
        @select="getWarehouseList"
        @close="showAddModal = false"
      />
    </a-row>
    <template slot="footer">
      <a-button type="primary"  @click="handleAdd">
        添加产品
      </a-button>
      <a-button :disabled="disabled" @click="cancel">
        取消
      </a-button>
      <a-button type="primary" :disabled="disabled" @click="confirm(true)">
        确定
      </a-button>
    </template>
    
  </ant-modal>
</template>
<script>
import BizProductAddForm from '@/views/biz/bizproduct/modules/BizProductAddForm';
import { listBizProduct, getInitData } from '@/api/biz/bizProduct'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import AntModal from '@/components/pt/dialog/AntModal'
import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
import { listTree, listTreeExcludeChild } from '@/api/biz/bizProductCategory'
import { allListBizWarehouse } from '@/api/biz/bizWarehouse'
export default {
  name: 'BizProduct',
  components: {
    AdvanceTable,
    AntModal,
    BizProductAddForm
  },
  mixins:[tableDragResizeMixin],
  data () {
    return {
      disabledCreated:true,
      productId:undefined,
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      bizProductList:[],
      inventoryOptions:[],
      warehouseList:[],
      categoryIdOptions:[],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        processName: undefined,
        inventoryFlag:'',
        processCode: undefined

      },
      columns: [
        {
          title: '产品大类',
          dataIndex: 'categoryName',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '产品编号',
          dataIndex: 'productCode',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '助记码',
          dataIndex: 'mnemonicCode',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '默认仓库',
          dataIndex: 'warehouseName',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '颜色',
          dataIndex: 'color',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '规格',
          dataIndex: 'spec',
          align: 'right',
          width: '12.8%'
        },
        {
          title: '型号',
          dataIndex: 'model',
          align: 'right',
          width: '12.8%'
        },
        {
          title: '库存',
          dataIndex: 'number',
          align: 'right',
          width: '12.8%'
        },
        {
          title: '单位',
          dataIndex: 'unit',
          align: 'right',
          width: '12.8%'
        },
        {
          title: '预警库存',
          dataIndex: 'warnStock',
          ellipsis: true,
          align: 'left',
          width: '12.8%'
        },
        {
          title: '库存计算',
          dataIndex: 'inventoryFlag',
          ellipsis: true,
          align: 'left',
          width: '12.8%',
          scopedSlots: {
            customRender: 'inventoryFlagSlot'
          }
        },
      ],
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      disabled: false,
      id: undefined
    }
  },
  created () {
    this.initColumnsSetting('productList')
    getInitData('inventory_flag').then(response => {
      this.inventoryOptions = response.data.inventory_flag
    })
  },
  methods: {
    /** 查询生产工序列表 */
    getList (list) {
      this.loading = true
      let excludeIds='';
      if(list && list.length>0) {
        list.forEach(item=>{
          if(excludeIds.length>0) {
            excludeIds+=",";
          }
          excludeIds+=item.id;
        });
      }
      if(excludeIds) {
        this.queryParam.excludeIds=excludeIds;
      }
      listBizProduct(this.queryParam).then(response => {
        this.bizProductList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    handleTableChange (pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },

    // 取消按钮
    cancel () {
      this.open = false
      this.$emit('close')
    },
    /** 提交按钮 */
    confirm: function (closeDialog) {
     // this.closeDialog = closeDialog
      this.disabled = true
      if(this.selectedRowKeys.length==0) {
        this.$message.warning('请选择产品');
        this.disabled = false
        return false
      } else {
        //保存产品
        this.$message.success('保存成功', 3)
        this.open = false
        this.$emit('ok',this.selectedRows);
        this.$emit('close')
        this.disabled = false
      }
    },
    handleAddProduct(list) {
      this.open = true;
      this.getList(list);
      
    this.getTreeSelect()
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,

        productCode: undefined,

        mnemonicCode: undefined

      }
      this.handleQuery()
    },
    // 添加产品
    handleAdd () {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizProductAddForm.handleAdd()
      ))
    },
    
    /** 查询菜单下拉树结构 */
    getTreeSelect (row) {
      if (row) {
        listTreeExcludeChild(row.id, '99').then(response => {
          this.categoryIdOptions=response.data;
        })
      } else {
        listTree('0', '99').then(response => {
          this.categoryIdOptions=response.data;
        })
      }
    },
    
    getWarehouseList() {
      allListBizWarehouse().then(response => {
        this.warehouseList = response.data
      })
    },
    /**
     * @添加产品成功
     * @param {string} mnemonicCode 
     */
    addProductOk(mnemonicCode){
      this.$set(this.queryParam,'orderParams',mnemonicCode)
      this.getList()
    },
  }
}
</script>
